package hash.leetcode;

import java.util.*;

/**
 * @Description: 最常见的单词
 * @Author Stark
 * @Date 2022/3/21 17:15
 **/
public class Num819_MostCommonWord {
    public static String mostCommonWord(String paragraph, String[] banned) {
        paragraph = paragraph.toLowerCase();
        //取出字符串中除了小写字母所有的字符，并按照多个空格才成字符串数组
        String[] strings = paragraph.replaceAll("[^a-z]"," ").split("\\s+");
        System.out.println(Arrays.toString(strings));
        Set<String> set = new HashSet<>();
        Map<String,Integer> map = new HashMap<>();
        for(String str:banned){
            set.add(str);
        }
        for (String tmp:strings) {
            if(!set.contains(tmp)){
                map.put(tmp,map.getOrDefault(tmp,0) + 1);
            }
        }
        String maxStr = "";
        int  max = 0;
        for(Map.Entry<String,Integer> tmp: map.entrySet()){
            if(tmp.getValue() > max){
                max = tmp.getValue();
                maxStr = tmp.getKey();
            }
        }
        return maxStr;
    }

    public static void main(String[] args) {
        String str = "a, a, a, a, b,b,b,c, c";
        String[] banned = new String[]{"a"};
        System.out.println(mostCommonWord(str,banned));
    }
}
